#include "FlowController.h"
#include "GameManager.h"
#include "Supporting/Enums/EnumScreenType.h"
#include "Screens/BaseScreen.h"
#include "Screens/MainMenuScreen.h"
#include "Screens/SkillSelectScreen.h"
#include "Screens/LevelSelectScreen.h"
#include "Skills/SkillsLoader.h"
#include "Levels/LevelLoader.h"

using namespace cocos2d;

FlowController::FlowController()
{
}

FlowController::~FlowController()
{
	delete myLevelLoader;
	myLevelLoader = NULL;

	delete mySkillsLoader;
	mySkillsLoader = NULL;
}

/*!
    @function loadInitialData
    @param		xSpeed:float
				speed to shift the map at
    @result		scrolls the map according to the xspeed. automatically rearranges the map when the map moves out of screen
 */
CCScene* FlowController::loadInitialScreen()
{
	myLevelLoader = new LevelLoader();
	mySkillsLoader = new SkillsLoader();

	BaseScreen *_mainMenuScreen = MainMenuScreen::createLayer();
	_mainMenuScreen->loadScreen(this);

	myBaseScreen = _mainMenuScreen;

	CCScene *scene = CCScene::create();
	scene->addChild(_mainMenuScreen);

	return scene;
}

/*
 * @brief: switch screen based on the ID given. Previous screen can still execute stuff before being released if applicable
 * @param screenID: ID of the screen to switch to
 * @result: Selected screen is loaded and displayed
 */
void FlowController::switchScreenTo(int screenID)
{
	//==================== Replace with new screen =========================
	if( screenID == SCREEN_GAMESCENE )
	{
		GameManager* newmyGameManager = GameManager::createLayer();
		newmyGameManager->loadGame();

		CCScene* scene = CCScene::create();
		scene->addChild(newmyGameManager);

		CCDirector::sharedDirector()->replaceScene(CCTransitionProgressHorizontal::create(0.5,scene));
	}
	else
	{
		BaseScreen *screen;
		if( screenID == SCREEN_SKILLSELECT )
			screen = SkillSelectScreen::createLayer();
		else if( screenID == SCREEN_LEVELSELECT )
			screen = LevelSelectScreen::createLayer();
		else if( screenID == SCREEN_MAIN )
			screen = MainMenuScreen::createLayer();

		screen->loadScreen(this);

		CCScene* scene = CCScene::create();
		scene->addChild(screen);

		if( screenID == SCREEN_MAIN )
			CCDirector::sharedDirector()->replaceScene(CCTransitionSlideInL::create(0.5,scene));
		else
			CCDirector::sharedDirector()->replaceScene(CCTransitionSlideInR::create(0.5,scene));
	}
}

